#include "Setting_Memory.h"
#include <QApplication>
#include <QSharedPointer>


namespace UserManagementSystem {
Setting_Memory::Setting_Memory(QWidget *parent)
    : QWidget{parent}
{
    qDebug()<<__FUNCTION__;

    //contentwidget放在outlayout中，scrollarea的内容放在contengwidget中
    m_contentWidget=new QWidget();
    m_outLayout=new QVBoxLayout(this);

    m_scrollArea=new QScrollArea();
    m_scrollArea->setAlignment(Qt::AlignLeft);
    m_scrollArea->setWidgetResizable(true);
    m_scrollArea->setWidget(m_contentWidget);
    m_scrollArea->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
    m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

    m_mainLayout=new QVBoxLayout(m_contentWidget);
    m_savePathBtn=new QPushButton();
    m_savePathFrame=new QFrame();


    m_outputBtnsGroup=new QButtonGroup();
    m_outputBtnsGroup->setExclusive(false);
    m_patientBtn=new QCheckBox();
    m_dateBtn=new QCheckBox();
    m_doctorBtn=new QCheckBox();
    m_typeBtn=new QCheckBox();

    m_backupPathBtn=new QPushButton();
    m_backupPathFrame=new QFrame();

    m_beginTimeBtn=new QPushButton();
    m_endTimeBtn=new QPushButton();
    m_backupBtn=new QPushButton();
    m_beginFrame=new QFrame();
    m_endFrame=new QFrame();

    m_exocadBtn=new QPushButton();
    m_exoPathFrame=new QFrame();
    m_beginSynch=new QPushButton();

    m_label_1.setText(tr("文件保存路径"));
    m_label_2.setText(tr("❓"));
    m_label_3.setText(tr("导出文件"));
    m_label_4.setText(tr("当前规则："));
    m_label_5.setText(tr("备份设置"));
    m_label_6.setText(tr("备份路径"));
    m_label_7.setText(tr("选择时间"));
    m_label_8.setText(tr("Exocad DentalDB路径"));
    m_label_9.setText(tr("同步数据"));
    m_label_10.setText(tr("未检测到需要同步的数据"));

    m_patientBtn->setText(tr("患者"));
    m_dateBtn->setText(tr("日期"));
    m_doctorBtn->setText(tr("医生"));
    m_typeBtn->setText(tr("类型"));

    m_beginTime.setText(tr("2024/01/01"));
    m_endTime.setText(tr("2024/01/01"));

    m_backupBtn->setText(tr("备份"));

    InitDefaultLayout();
    BindSignalsToSlots();

}

void Setting_Memory::On_SavePathBtn_clicked()
{




    QString choose_path=QFileDialog::getExistingDirectory(nullptr,tr("选择文件夹"));
    qDebug()<<"选择的文件路径为："<<choose_path;
    m_savePathLabel.setText(choose_path);

}

void Setting_Memory::On_BackupPathBtn_clicked()
{
    QString choose_path=QFileDialog::getExistingDirectory(nullptr,tr("选择文件夹"));
    qDebug()<<"选择的文件路径为："<<choose_path;
    m_backupLabel.setText(choose_path);
}

void Setting_Memory::On_ExoPathBtn_clicked()
{
    QString choose_path=QFileDialog::getExistingDirectory(nullptr,tr("选择文件夹"));
    qDebug()<<"选择的文件路径为："<<choose_path;
    m_exocadPathLabel.setText(choose_path);
}

void Setting_Memory::On_BeginTimeBtn_Clicked()
{
    QString begintime="";
    QCalendarWidget*  calendar=new QCalendarWidget();
    calendar->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);

    QPoint pos=m_beginTimeBtn->mapToGlobal(m_beginTimeBtn->rect().bottomLeft());
    calendar->move(pos);
    calendar->show();

    connect(calendar,&QCalendarWidget::selectionChanged,this,[this,calendar](){
        m_beginTime.setText(calendar->selectedDate().toString());
    });


}

void Setting_Memory::On_EndTimeBtn_Clicked()
{

    QString endtime="";
    QCalendarWidget*  calendar=new QCalendarWidget();
    calendar->setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);

    QPoint pos=m_beginTimeBtn->mapToGlobal(m_beginTimeBtn->rect().bottomLeft());
    calendar->move(pos);
    calendar->show();

    connect(calendar,&QCalendarWidget::selectionChanged,this,[this,calendar](){
        m_endTime.setText(calendar->selectedDate().toString());
    });

}

void Setting_Memory::InitDefaultLayout()
{

    QHBoxLayout* sub_layout_1=new QHBoxLayout();
    sub_layout_1->addWidget(&m_label_1);
    sub_layout_1->addWidget(&m_label_2);
    sub_layout_1->addStretch(1);

    QHBoxLayout* sub_layout_2=new QHBoxLayout(m_savePathFrame);
    m_savePathBtn->setFixedSize(30,30);
    sub_layout_2->addWidget(&m_savePathLabel);
    sub_layout_2->addWidget(m_savePathBtn);
    sub_layout_2->setContentsMargins(0,0,0,0);

    QHBoxLayout* sub_layout_3= new QHBoxLayout();
    sub_layout_3->addWidget(m_patientBtn);
    sub_layout_3->addWidget(m_dateBtn);
    sub_layout_3->addWidget(m_doctorBtn);
    sub_layout_3->addWidget(m_typeBtn);
    sub_layout_3->addStretch(1);
    m_outputBtnsGroup->addButton(m_patientBtn);
    m_outputBtnsGroup->addButton(m_dateBtn);
    m_outputBtnsGroup->addButton(m_doctorBtn);
    m_outputBtnsGroup->addButton(m_typeBtn);


    QHBoxLayout* sub_layout_4=new QHBoxLayout(m_backupPathFrame);
    sub_layout_4->addWidget(&m_backupLabel);
    sub_layout_4->addWidget(m_backupPathBtn);
    m_backupPathBtn->setFixedSize(30,30);
    sub_layout_4->setContentsMargins(0,0,0,0);

    QHBoxLayout* sub_layout_5=new QHBoxLayout(m_beginFrame);
    QHBoxLayout* sub_layout_6=new QHBoxLayout(m_endFrame);
    QHBoxLayout* sub_layout_7=new QHBoxLayout();
    sub_layout_5->setContentsMargins(0,0,0,0);
    sub_layout_6->setContentsMargins(0,0,0,0);

    QHBoxLayout* sub_layout_8=new QHBoxLayout(m_exoPathFrame);
    sub_layout_8->addWidget(&m_exocadPathLabel);
    sub_layout_8->addWidget(m_exocadBtn);
    m_exocadBtn->setFixedSize(30,30);
    sub_layout_8->setContentsMargins(0,0,0,0);


    sub_layout_5->addWidget(m_beginTimeBtn);
    sub_layout_5->addWidget(&m_beginTime);

    sub_layout_6->addWidget(m_endTimeBtn);
    sub_layout_6->addWidget(&m_endTime);


    sub_layout_7->addWidget(m_beginFrame);
    sub_layout_7->addWidget(new QLabel("-"));
    sub_layout_7->addWidget(m_endFrame);
    sub_layout_7->addStretch(1);
    sub_layout_7->addWidget(m_backupBtn);

    m_mainLayout->addLayout(sub_layout_1);
    m_mainLayout->addWidget(m_savePathFrame);
    m_mainLayout->addSpacing(10);
    m_mainLayout->addWidget(&m_label_3);
    m_mainLayout->addLayout(sub_layout_3);
    m_mainLayout->addWidget(&m_label_4);
    m_mainLayout->addSpacing(10);
    m_mainLayout->addWidget(&m_label_5);
    m_mainLayout->addWidget(&m_label_6);
    m_mainLayout->addWidget(m_backupPathFrame);
    m_mainLayout->addSpacing(10);
    m_mainLayout->addWidget(&m_label_7);
    m_mainLayout->addLayout(sub_layout_7);
    m_mainLayout->addWidget(&m_label_8);
    m_mainLayout->addWidget(m_exoPathFrame);
    m_mainLayout->addSpacing(10);
    m_mainLayout->addWidget(&m_label_9);
    m_mainLayout->addWidget(&m_label_10);


    m_outLayout->addWidget(m_scrollArea);
    InitDefaultStyle();

}

void Setting_Memory::InitDefaultStyle()
{
    QString label_style=(R"(
        font-size:9pt;color: black; font-weight: bold;
    )");
    m_label_1.setStyleSheet(label_style);
    m_label_3.setStyleSheet(label_style);
    m_label_5.setStyleSheet(label_style);
    m_label_8.setStyleSheet(label_style);
    m_label_9.setStyleSheet(label_style);

    m_label_4.setStyleSheet("color:gray;");
    m_label_6.setStyleSheet("color:gray;");
    m_label_7.setStyleSheet("color:gray;");
    m_label_10.setStyleSheet("color:gray;");

    //frame的样式
    QString frame_style=R"(

    background-color: #f7f9fb;
    border: 1px solid #f7f9fb;
    border-radius: 4px;
    padding :0px;
)";

    m_savePathFrame->setStyleSheet(frame_style);
    m_savePathFrame->setFixedHeight(35);

    m_backupPathFrame->setStyleSheet(frame_style);
    m_backupPathFrame->setFixedHeight(35);

    m_beginFrame->setStyleSheet(frame_style);
    m_endFrame->setStyleSheet(frame_style);
    m_beginFrame->setFixedHeight(35),m_endFrame->setFixedHeight(35);

    m_exoPathFrame->setStyleSheet(frame_style);
    m_exoPathFrame->setFixedHeight(35);

    QString tip_path=QApplication::applicationDirPath()+"/res/PatientManager/tip.png";
    m_label_2.setPixmap(QPixmap(tip_path).scaled(20,20));


    QString open_folder=QApplication::applicationDirPath()+"/res/PatientManager/open_folder.png";
    QString open_folder_hover=QApplication::applicationDirPath()+"/res/PatientManager/open_folder_hover.png";

    QString calender_path=QApplication::applicationDirPath()+"/res/PatientManager/calender.png";
    QString calender_path_hover=QApplication::applicationDirPath()+"/res/PatientManager/calender_hover.png";


    this->LoadIcon(m_savePathBtn,open_folder,open_folder_hover);
    this->LoadIcon(m_backupPathBtn,open_folder,open_folder_hover);
    this->LoadIcon(m_beginTimeBtn,calender_path,calender_path_hover);
    this->LoadIcon(m_endTimeBtn,calender_path,calender_path_hover);
    this->LoadIcon(m_exocadBtn,open_folder,open_folder_hover);


    //scrollarea样式
    //滚动条样式
    QString scroll_sty=(R"(
    QScrollArea{
    border:none;
}

    QScrollArea  QScrollBar:vertical,QScrollArea  QScrollBar:horizontal {
        background-color: white;
        width: 4px;
        margin: 0px 0px 0px 0px;
    }

    QScrollArea QScrollBar::handle:vertical,QScrollArea QScrollBar::handle:horizontal {
        background: gray;
        height: 6px;
        width: 4px;
        border-radius: 2px;
    }

    QScrollArea QScrollBar::sub-line, QScrollArea  QScrollBar::add-line,
    QScrollArea QScrollBar::up-arrow, QScrollArea  QScrollBar::down-arrow:vertical {
        height: 0px;
    }
)");
    m_scrollArea->setStyleSheet(scroll_sty);
    m_contentWidget->setStyleSheet("background-color: white;");

    //按钮的样式
    QString backup_style=R"(
    QPushButton{
        border:1px solid #4672C4;
        height:30px;
        width:70px;
        border-radius:15px;
        background-color:white;
        color:#4672C4;
    }
    QPushButton::hover{
        background-color:#4672C4;
        color:white;
    }
)";


    m_backupBtn->setStyleSheet(backup_style);

}

void Setting_Memory::LoadIcon(QWidget *btn, const QString path_1, const QString path_2)
{
    QString style=QString(R"(
    QPushButton {
        border: none;
        image: url(%1);
    }
    QPushButton:hover {
        image: url(%2);
    }
)").arg(path_1).arg(path_2);

    btn->setStyleSheet(style);

}

void Setting_Memory::BindSignalsToSlots()
{
    connect(m_savePathBtn,&QPushButton::clicked,this,&Setting_Memory::On_SavePathBtn_clicked);

    connect(m_backupPathBtn,&QPushButton::clicked,this,&Setting_Memory::On_BackupPathBtn_clicked);

    connect(m_exocadBtn,&QPushButton::clicked,this,&Setting_Memory::On_ExoPathBtn_clicked);

    connect(m_beginTimeBtn,&QPushButton::clicked,this,&Setting_Memory::On_BeginTimeBtn_Clicked);

    connect(m_endTimeBtn,&QPushButton::clicked,this,&Setting_Memory::On_EndTimeBtn_Clicked);



}

}

